package cn.itcast.service.impl;

import cn.itcast.dao.SetmealDao;
import cn.itcast.entity.QueryPageBean;
import cn.itcast.massage.RedisConstant;
import cn.itcast.pojo.Setmeal;
import cn.itcast.service.SetmealService;
import com.alibaba.dubbo.config.annotation.Service;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import freemarker.template.Configuration;
import freemarker.template.Template;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.servlet.view.freemarker.FreeMarkerConfigurer;
import redis.clients.jedis.JedisPool;

import java.io.File;
import java.io.FileWriter;
import java.io.IOException;
import java.io.Writer;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service(interfaceClass = SetmealService.class)
@Transactional
public class SetmealServiceImpl implements SetmealService {

    @Autowired
    private JedisPool jedisPool;
    @Autowired
    private SetmealDao setmealDao;
    @Autowired
    private FreeMarkerConfigurer configurer;
    @Value("${out_put_path}")
    private String outPutPath;
    @Override
    public void add(Setmeal setmeal, Integer[] checkgroupIds) {
        setmealDao.add(setmeal);
        Integer setmealId = setmeal.getId();
        for (Integer checkgroupId : checkgroupIds) {
            Map<String,Integer> map = new HashMap<>();
            map.put("setmealId",setmealId);
            map.put("checkgroupId",checkgroupId);
            setmealDao.addCheckgroup(map);
        }
        // 添加完成, 将图片名添加到jedis的小set集合中,目的是做定时垃圾清理
        if (setmeal.getImg()!=null) {
            jedisPool.getResource().sadd(RedisConstant.SETMEAL_PIC_DB_RESOURCES, setmeal.getImg());
        }

        // 调用生成静态页面的方法
        generateMobileStaticHtml();
    }

    private void generateMobileStaticHtml() {
        List<Setmeal> list = setmealDao.findAll();
        // 生成套餐列表静态页面
        generateMobileSetmealListStaticHtml(list);
        // 生成套餐详情静态页面
        generateMobileSetmealStaticHtml(list);
    }

    private void generateMobileSetmealStaticHtml(List<Setmeal> list) {
        for (Setmeal setmeal : list) {
            Integer id = setmeal.getId();
            Setmeal sm = setmealDao.findSetmealById(id);
            if(sm==null){
                sm = setmealDao.findById(id);
            }
            Map map = new HashMap();
            map.put("setmeal",sm);
            freeMarkerMethod("mobile_setmeal_detail.ftl","m_setmeal_detail_"+id+".html",map);
        }

    }

    private void generateMobileSetmealListStaticHtml(List<Setmeal> list) {
        Map map = new HashMap();
        map.put("setmealList",list);
        freeMarkerMethod("mobile_setmeal.ftl","m_setmeal.html",map);
    }

    public void freeMarkerMethod(String ftlName, String htmlPageName,Map dataModel) {
        Configuration configuration = configurer.getConfiguration();
        Writer writer = null;
        try {
            Template template = configuration.getTemplate(ftlName);
            writer = new FileWriter(outPutPath + "/" + htmlPageName);
            template.process(dataModel, writer);
        }catch (Exception e){
            e.printStackTrace();
        }finally {
            if (writer!=null){
                try {
                    writer.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
    }

    @Override
    public PageInfo findPage(QueryPageBean queryPageBean) {
        Integer currentPage = queryPageBean.getCurrentPage();
        Integer pageSize = queryPageBean.getPageSize();
        String queryString = queryPageBean.getQueryString();
        PageHelper.startPage(currentPage,pageSize);
        List<Setmeal> list = setmealDao.findPage(queryString);
        PageInfo<Setmeal> pageInfo = new PageInfo<>(list);
        return pageInfo;
    }

    @Override
    public List<Setmeal> findAll() {
        return setmealDao.findAll();
    }

    @Override
    public Setmeal findSetmealById(Integer id) {
        return setmealDao.findSetmealById(id);
    }

    @Override
    public Setmeal findById(Integer id) {
        return setmealDao.findById(id);
    }

    @Override
    public List<Map> getSetmealReport() {
        return setmealDao.getSetmealReport();
    }
}
